package com.audaque.springboot.foshanupload.security.interceptor;

import com.audaque.springboot.foshanupload.authcore.model.currentUser.TransmittableThreadLocalCurrentUser;
import com.audaque.springboot.foshanupload.security.util.SecurityContextAuthentication2TransmittableThreadLocalCurrentUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


//通过配置order()的值，值越小，优先级越高。不配默认是0
@Order(100)
@Slf4j
public class SecurityContextAuthentication2TransmittableThreadLocalCurrentUserInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        TransmittableThreadLocalCurrentUser transmittableThreadLocalCurrentUser = TransmittableThreadLocalCurrentUser.getCurrentUser();
        log.debug("transmittableThreadLocalCurrentUser:"+transmittableThreadLocalCurrentUser);
        if(transmittableThreadLocalCurrentUser==null){
            SecurityContextAuthentication2TransmittableThreadLocalCurrentUserUtil.covert();
             transmittableThreadLocalCurrentUser = TransmittableThreadLocalCurrentUser.getCurrentUser();
            log.debug("transmittableThreadLocalCurrentUser:"+transmittableThreadLocalCurrentUser);
        }


        return true;
    }
}
